/**
 * 
 */
package z.tool.dao;

import java.util.Map;

import z.tool.util.Page;
import z.tool.util.ZUtils;

/**
 * @author auzll@163.com
 */
public class QueryCondition<T> {
    /** 默认情况下，每次最多获取15条记录  */
    public static final int DEFAULT_LIMIT = 15;
    
    /** 所访问的实体 */
    private T entity;
    
    /** 每页最多显示的记录数量 */
    private int limit = DEFAULT_LIMIT;
    
    /** 其他查询参数 */
    private Map<String, Object> others;
    
    /** 分页参数 */
    private int start;
    
    public QueryCondition() {}
    
    public QueryCondition(Page page) {
        this(null, page, null);
    }
    
    public QueryCondition(T entity) {
        this(entity, null, null);
    }
    
    public QueryCondition(T entity, Page page) {
        this(entity, page, null);
    }

    public QueryCondition(T entity, Page page, Map<String, Object> others) {
        this.entity = entity;
        
        if (null != page) {
            this.limit = page.getPageMax();
            int startPage = page.getPage();
            if (startPage < 1) {
                startPage = 1;
            }
            this.start = (startPage - 1) * limit;
        }
        
        this.others = others;
    }
    
    public QueryCondition<T> addOthers(Map<String, Object> others) {
        if (null == this.others) {
            this.others = ZUtils.newHashMap();
        }
        this.others.putAll(others);
        return this;
    }

    public QueryCondition<T> addOthers(String key, Object value) {
        if (null == others) {
            others = ZUtils.newHashMap();
        }
        others.put(key, value);
        return this;
    }

    public T getEntity() {
        return entity;
    }
    
    public int getLimit() {
        return limit;
    }

    public Map<String, Object> getOthers() {
        return others;
    }

    public int getStart() {
        return start;
    }
}
